package customer

import (
	"context"
	"fmt"
	"github.com/google/wire"
	"go.manyji.com/lib/log"
	"sieve_admin_server/app/api/util"
	"sieve_admin_server/app/contextx"
	"sieve_admin_server/app/schema"
	"sieve_admin_server/app/server"
	serviceCustomer "sieve_admin_server/app/service/customer"
)

var (
	_           util.API = (*RechargeAPI)(nil)
	RechargeSet          = wire.NewSet(wire.Struct(new(RechargeAPI), "*"))
)

type RechargeAPI struct {
	util.EmptyAPI
	RechargeSrv *serviceCustomer.RechargeSrv
}

func (a *RechargeAPI) GetApis() []*server.BatchModTree {
	return []*server.BatchModTree{
		{
			Obj:   a,
			Name:  "recharge",
			Title: "充值管理", Perms: []*server.Perm{
				{Name: "query", Title: "搜索"},
				{Name: "add", Title: "充值/退款"},
				{Name: "export", Title: "充值/退款"},
			},
			Order: 8,
		},
	}
}

func (a *RechargeAPI) API_add_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("account_id", true),      // 必传
				contextx.NewVar("recharge_points", true), // 充值必传
				contextx.NewVar("bonus_points", true),    // 充值必传
				contextx.NewVar("refund_points", true),   // 退款必传
				contextx.NewVar("type", true),            // 1 表示充值，2 表示退款
				contextx.NewVar("refund_ty", true),       // 1 表示扣减充值账户余额，2 表示扣减赠送账户余额 （默认为1）
			},
		)
	)

	return a.RechargeSrv.Create(context.Background(), uint64(ctx.Uid), reqArgs)
}

func (a *RechargeAPI) API_query_list(ctx *server.Context) interface{} {
	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("creator", true),
				contextx.NewVar("account_id", true),
				contextx.NewVar("leader_id", true),
				contextx.NewVar("creator", true),
				contextx.NewVar("start", true),
				contextx.NewVar("end", true),
			},
		)
		p = schema.PaginationParam{
			PageIndex: util.ToInt(ctx.Args.Get("page_index")),
			PageSize:  util.ToInt(ctx.Args.Get("page_size")),
		}
	)

	return a.RechargeSrv.Query(context.Background(), uint64(ctx.Uid), reqArgs, p)
}

func (a *RechargeAPI) API_export_list(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("ids"),  // recharge 表的主键ID，多个 用 "|" 拼接
				contextx.NewVar("seqs"), // 当前查询条件下的顺序ID，多个 用 "|" 拼接
			},
		)
	)

	return a.RechargeSrv.Export(context.Background(), uint64(ctx.Uid), reqArgs, func(fileName string) {

		log.Info(fmt.Sprintf("export file %v", fileName))
		//http.ServeFile(ctx.Http.ResponseWriter, ctx.Http.Request, fileName)
	})
}
